package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@WebServlet("/ip")
public class IpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String,Integer> map=new HashMap<>();
        InputStream resourceAsStream = getClass().getResourceAsStream("/ip.txt");
        InputStreamReader inputStreamReader=new InputStreamReader(resourceAsStream);
        BufferedReader br=new BufferedReader(inputStreamReader);
        String line=null;
        while((line=br.readLine())!=null){
            if(!map.containsKey(line)){
            map.put(line,1);
            }else{
                Integer integer=map.get(line);
                integer++;
                map.put(line,integer);
            }
        }
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey()+"---"+entry.getValue());
            resp.getWriter().print(entry.getKey()+"---"+entry.getValue());
            resp.getWriter().print("\t\n");
        }
    }


}
